package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.Employee;
import com.atguigu.springcloud.page.SimplePage;
import com.atguigu.springcloud.request.EmployeePageRequest;
import com.atguigu.springcloud.request.SaveEmployeeRequest;
import com.atguigu.springcloud.response.BaseResponse;
import com.atguigu.springcloud.response.EmployeeVO;
import com.atguigu.springcloud.response.PageResponse;
import com.atguigu.springcloud.response.PageResponseDetail;
import com.atguigu.springcloud.service.EmployeeService;
import com.atguigu.springcloud.utils.ModelMapperUtil;
import io.swagger.annotations.*;
import org.modelmapper.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * Created by luojie on 2018/9/7.
 */
@RestController("employeeController")
@Api(tags = "员工信息接口")
@CrossOrigin //跨域访问 swagger 要使用到
public class EmployeeController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("新增员工")
    @PostMapping(value = "/employee/save")
    @ApiResponses({@ApiResponse(code = 200, message = "保存成功"),
            @ApiResponse(code = 400, message = "错误的请求")})
    public BaseResponse save(@ApiParam @RequestBody @Validated SaveEmployeeRequest request, HttpServletResponse response){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");

        logger.info("保存员工 [uuid:{}, request:{}]", uuid, request);
        Employee employee = employeeService.save(request);
        logger.info("保存员工 ，成功[uuid:{}, employee:{}]", uuid, employee);
        return successSaveBaseResponse();
    }

    @ApiOperation("分页获取员工信息")
    @RequestMapping(value = "page", method = RequestMethod.POST)
    public PageResponse<EmployeeVO> page(@ApiParam @Validated @RequestBody EmployeePageRequest pageRequest) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        logger.info("分页获取员工信息[uuid:{}, pageRequest:{}]", uuid, pageRequest);

        //分页查询数据
        SimplePage<Employee> simplePage = employeeService.appPage(pageRequest);

        //转换数据
        List<Employee> parkingSystemSupplierEOS = simplePage.getContent();
        List<EmployeeVO> employeeVOS = ModelMapperUtil.get().map(parkingSystemSupplierEOS, new TypeToken<List<EmployeeVO>>() {
        }.getType());



        PageResponseDetail responseDetail = simplePage.convertToPageResponseDetail();

        logger.info("分页获取员工信息成功[uuid:{}, pageRequest:{}]", uuid, pageRequest);
        return successQueryPageResponse(responseDetail, employeeVOS);
    }

    @ApiOperation("根据id查找")
    @GetMapping("/employee/get/{id}")
    public Employee get(@PathVariable("id") String id){

        return employeeService.findById(id);
    }

    @ApiOperation("保存员工")
    @PostMapping("/employee/add")
    public Employee add(@RequestBody Employee employee){
        return employeeService.save(employee);
    }

}
